﻿using System;


namespace Client.Framework.Util
{
    /// <summary>
    /// Author: Leil
    /// Description: 字节操作通用类
    /// CreateTime: 2020/5/11 17:02:49
    /// </summary>
    public class BufferUtil
    {
        /// <summary>
        /// 从字节数组种获取一个int的值
        /// </summary>
        /// <param name="buffer">字节数组</param>
        /// <param name="offset">字节数组中的偏移量</param>
        /// <returns>获取的int值</returns>
        public static int GetInt(byte[] buffer, int offset)
        {
            byte[] data = new byte[4];
            Array.Copy(buffer, offset, data, 0, 4);
            return BitConverter.ToInt32(data, 0);
        }

        /// <summary>
        /// 从字节数组种获取一个float的值
        /// </summary>
        /// <param name="buffer">字节数组</param>
        /// <param name="offset">字节数组中的偏移量</param>
        /// <returns>获取的float值</returns>
        public static float GetFloat(byte[] buffer, int offset)
        {
            byte[] data = new byte[4];
            Array.Copy(buffer, offset, data, 0, 4);
            return BitConverter.ToSingle(data, 0);
        }

    }
}
